package com.wenlin.myproduction.ui;

import android.util.Log;

import com.wenlin.myproduction.bean.Sub;

import java.util.ArrayList;
import java.util.List;

public class PuzzleClass {
    private static PuzzleClass instance =null;

    private PuzzleClass() {
    }

    public static PuzzleClass getInstance() {
        synchronized (PuzzleClass.class) {
            if (instance == null) {
                instance = new PuzzleClass();
            }
        }
        return instance;
    }

    public static void figureOut() {
        String arr[] = {"呼和浩特", "北京", "齐齐哈尔", "大连", "上海滩"};
        List<String> list;
        list = new ArrayList<>();
        for (String str :
                arr) {
            list.add(str);
        }

        String input = "呼特13:44    23:50";

        List<String> outList = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            String ori = list.get(i);
            int index = -1;
            boolean has = true;
            if (input.length() > 1) {
                for (int j = 0; j < input.length(); j++) {
                    String c = input.substring(j, j + 1);
                    int index2 = ori.indexOf(c);
                    if (index2 > -1 && index2 > index) {
                        index = index2;
                    } else {
                        has = false;
                        break;
                    }
                }
            }
            if (has) {
                outList.add(ori);
            }
        }
        //输出结果
        if (outList.size() > 0) {
            for (int i = 0; i < outList.size(); i++) {
                Log.e("result", outList.get(i));
            }
        } else {
            Log.e("result", "empty");
        }
    }

    public static void figureOut2() {
        List<Sub> list = new ArrayList<>();
        list.add(new Sub("总经办", ""));
        list.add(new Sub("财务部", "总经办"));
        list.add(new Sub("审计部", "财务部"));
        list.add(new Sub("现金部", "财务部"));
        list.add(new Sub("会计部", "财务部"));
        list.add(new Sub("华北区", "审计部"));
        list.add(new Sub("华中区", "审计部"));

        String input = "总经办";
//        String input = "财务部";
//        String input = "审计部";


        List<String> outList = new ArrayList<>();
        StringBuilder key = new StringBuilder(input);
        //因为list是从总经办由上至下添加的，所以可以在发现父部门是input时再开始下层循环即可
        boolean findTarget = false;
        for (int i = 0; i < list.size(); i++) {
            Sub sub = list.get(i);
            String name = sub.getName();
            if (name.length() > 0 && key.toString().contains(name)) {
                findTarget = true;
            }
            if (findTarget && key.toString().contains(sub.getBelong())) {
                if (!outList.contains(name)) {
                    outList.add(name);
                }
                if (!key.toString().contains(name)) {
                    key.append(",").append(name);
                }
            }
        }
        //输出结果
        if (outList.size() > 0) {
            for (int i = 0; i < outList.size(); i++) {
                Log.e("result", outList.get(i));
            }
        } else {
            Log.e("result", "empty");
        }
    }

}
